package com.baitengsoft.patentapimonitor.api.controller;

import com.baitengsoft.patentapimonitor.api.entity.WebUserinfoEntity;
import com.baitengsoft.patentapimonitor.api.model.CfgPatentIndicatorInfo;
import com.baitengsoft.patentapimonitor.api.model.param.SearchPatentIndicatorParam;
import com.baitengsoft.patentapimonitor.api.service.CfgPatentIndicatorService;
import com.baitengsoft.patentapimonitor.common.utils.PageResult;
import com.baitengsoft.patentapimonitor.common.utils.Res;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import springfox.documentation.annotations.ApiIgnore;

import java.util.List;


/**
 * 指标管理相关
 * @author Chen 2017/12/15
 */
@RestController
@ResponseBody
@RequestMapping("/api/v1/web")
@Api(value = "前台-指标管理",description = "指标管理")
public class WebPatentIndicatorController extends AbstractController {

    @Autowired
    private CfgPatentIndicatorService cfgPatentIndicatorService;

    /**
     * 根据平台Id获取该平台的汇总
     */
    @ApiOperation(value = "获取年度指标汇总")
    @GetMapping("/sys/patindicatorsum")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "query", dataType = "Integer", name = "pageNumber", value = "页号", required = true),
            @ApiImplicitParam(paramType = "query", dataType = "Integer", name = "pageSize", value = "每页多少条", required = true)
    })
    public Res patentIndicatorList(@ApiIgnore SearchPatentIndicatorParam param){
        if (param.getPageNumber()== null|| param.getPageSize()==null){
            return Res.error("请求参数有误");
        }
        WebUserinfoEntity userInfo = getUserInfo();
        if(userInfo ==null){
            return Res.error("登录信息失效");
        }
        param.setPlatformID(userInfo.getPlatformid());
        PageResult<CfgPatentIndicatorInfo> result=cfgPatentIndicatorService.getPatentIndicatorSum(param);
        return Res.ok().put("list",result);
    }

    /**
     * 根据年份获取年度指标详情
     * @param param
     * @return
     */
    @ApiOperation(value = "获取年度指标详情")
    @GetMapping("/sys/patindicator")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "query", dataType = "Integer", name = "pageNumber", value = "页号", required = true),
            @ApiImplicitParam(paramType = "query", dataType = "Integer", name = "pageSize", value = "每页多少条", required = true),
            @ApiImplicitParam(paramType = "query", dataType = "Integer", name = "year", value = "年份", required = true),
    })
    public Res patentIndicatorDetail(@ApiIgnore SearchPatentIndicatorParam param){
        if (param.getYear()==null){
            return Res.error("请求参数有误");
        }
        WebUserinfoEntity userInfo = getUserInfo();
        if(userInfo ==null){
            return Res.error("登录信息失效");
        }
        param.setPlatformID(userInfo.getPlatformid());
        PageResult<CfgPatentIndicatorInfo> result=cfgPatentIndicatorService.getPatentIndicatorList(param);
        return Res.ok().put("list",result);
    }


    @ApiOperation(value = "修改指标")
    @PostMapping("/sys/patentIndicatormodify")
    public Res patentIndicatormodify(@RequestBody List<CfgPatentIndicatorInfo> info) throws Exception{
        if (info == null){
            return Res.ok();
        }
        WebUserinfoEntity userInfo = getUserInfo();
        if(userInfo ==null){
            return Res.error("登录信息失效");
        }
        return cfgPatentIndicatorService.updatePatentIndicator(info,userInfo.getPlatformid());
    }
}
